package com.qf.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.common.goods.GoodsState;
import com.qf.common.page.PageInfo;
import com.qf.common.page.PageUtils;
import com.qf.common.response.Response;
import com.qf.entity.dto.Goods;
import com.qf.entity.dto.GoodsType;
import com.qf.service.GoodsService;
import com.qf.service.GoodsTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@RequestMapping("goods")
@Controller
public class GoodsController {

    @Resource
    GoodsService goodsService;
    @Resource
    GoodsTypeService goodsTypeService;
    /**
     * 前往商品页面，携带商品类型数据和商品信息以及分页信息
     */
    @RequestMapping("list")
    public String toGoodsList(Model model,@RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage, @RequestParam(value = "goodsTypeId",required = false) String goodsTypeId){
        //获取商品类型列表
        QueryWrapper<GoodsType> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(GoodsType::getOrderNum);
        model.addAttribute("goodsTypes",goodsTypeService.list(wrapper));

        //获取商品列表并分页
        QueryWrapper<Goods> goodsWrapper = new QueryWrapper<>();
        if ((goodsTypeId !=null) && (!goodsTypeId.equals(""))) {
            goodsWrapper.lambda()
                        .eq(Goods::getTypeId, Long.parseLong(goodsTypeId))
                        .orderByDesc(Goods::getCreateTime);
            model.addAttribute("isExistTypeId","1");
        }else{
            goodsWrapper.lambda().orderByDesc(Goods::getCreateTime);
            model.addAttribute("isExistTypeId","0");
        }
        PageInfo<Goods> pageInfo = PageUtils.getPageList(currentPage, goodsWrapper, goodsService);
        model.addAttribute("goodsList",pageInfo.getPageRecords());
        model.addAttribute("pageCounts",pageInfo.getPageCounts());
        return "goods/list";
    }

    /**
     * 根据商品id对其进行上架
     * @param goodsId
     * @return
     */
    @RequestMapping("up")
    @ResponseBody
    public Response<?> up(@RequestParam("id")Long goodsId){
        goodsService.upByGoodsId(goodsId);
        return Response.success();
    }

    /**
     * 根据商品id对其进行下架
     * @param goodsId
     * @return
     */
    @RequestMapping("down")
    @ResponseBody
    public Response<?> down(@RequestParam("id")Long goodsId){
        goodsService.downByGoodsId(goodsId);
        return Response.success();
    }

    /**
     * 根据商品id对其进行删除
     * @param goodsId
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    public Response<?> del(@RequestParam("id")Long goodsId){
        goodsService.removeById(goodsId);
        return Response.success();
    }
}
